package BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class binaryTree {

    static class TreeNode {
        private char val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode creatTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
//        TreeNode I = new TreeNode('I');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.left = H;
//        H.right = I;
        return A;

    }

    public void preOrder(TreeNode root){
        if(root==null){
            return;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        preOrder(root.left);
        System.out.println(root.val);
        preOrder(root.right);
    }

    public void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.println(root.val);
    }

    List<TreeNode>  list = new ArrayList<>();
    public List<TreeNode> preOrder2(TreeNode root){
        if(root==null){
            return null;
        }

        list.add(root);
        preOrder2(root.left);
        preOrder2(root.right);
        return list;
    }

    public List<TreeNode> preOrder3(TreeNode root){
        List<TreeNode>  ret = new ArrayList<>();

        if(root==null){
            return ret;
        }
        //根
        ret.add(root);
        //左
        List<TreeNode> retleft = preOrder3(root.left);
        ret.addAll(retleft);
        //右
        List<TreeNode> retright = preOrder3(root.right);
        ret.addAll(retright);

        return ret;
    }

    public List<TreeNode> inOrder2(TreeNode root){
        List<TreeNode>  ret = new ArrayList<>();

        if(root==null){
            return ret;
        }

        List<TreeNode> retleft = preOrder2(root.left);
        ret.addAll(retleft);

        ret.add(root);

        List<TreeNode> retright = preOrder2(root.right);
        ret.addAll(retright);

        return ret;
    }

    public List<TreeNode> postOrder2(TreeNode root){
        List<TreeNode>  ret = new ArrayList<>();

        if(root==null){
            return ret;
        }

        List<TreeNode> retleft = preOrder2(root.left);
        ret.addAll(retleft);

        List<TreeNode> retright = preOrder2(root.right);
        ret.addAll(retright);

        ret.add(root);

        return ret;
    }

}
